/* 
Table 4: IV Estimates of the Effect of the HLG on Homeownership by Race and Region (1960)
Figure 7: IV Estimates of Effect of Home Loan Guaranty on Homeownership by Region (1960)

Chinyere O Agbai 

*/

global dir = "YourFilePath"
global output = "$dir"
global working_data = "$dir/ipums60_pub.dta"

cd "$output"
use "$working_data", clear

set scheme plotplain

* State Fixed Effects Global 
global state_fe state_cat2 state_cat3 state_cat4 state_cat5 state_cat6 ///
	state_cat7 state_cat8 state_cat9 state_cat10 state_cat11 state_cat12 state_cat13 ///
	state_cat14 state_cat15 state_cat16 state_cat17 state_cat18 state_cat19 state_cat20 ///
	state_cat21 state_cat22 state_cat23 state_cat24 state_cat25 state_cat26 state_cat27 ///
	state_cat28 state_cat29 state_cat30 state_cat31 state_cat32 state_cat33 state_cat34 /// 
	state_cat35 state_cat36 state_cat37 state_cat38 state_cat39 state_cat40 state_cat41 ///
	state_cat42 state_cat43 state_cat44 state_cat45 state_cat46 state_cat47 state_cat48 ///
	state_cat49 state_cat50

********************************************************************************************************
* Table 4: IV Estimates of the of Linkages between HLG and Logged Home Value by Race and Region (1960) *
********************************************************************************************************
set more off
putexcel set T4.xls, replace
	
putexcel B2 = ("All")  C2 = ("White")  D2 = ("Black") E2 = ("N")
putexcel A3 = ("Midwest") A5= ("Northeast") 
putexcel A7 = ("South") A9 = ("West") A11 = ("N")
putexcel A1 = ("IV Estimates of the Effect of Veteran Status on Home Value by Race and Region, 1912-1932 Birth Cohorts (1960)")
putexcel A11 = ("Robust standard errors in parentheses. Models include state fixed effects")
putexcel A12 = ("*** p<0.001, ** p<0.01, * p<0.05, + p <0.10")	
	

* FULL SAMPLE ******************************************************************
forvalues k = 1/4 {
	ivreg ln_valueh_2018 age $state_fe (vetbin_60= inst_1927) [weight = perwt] if region_no == `k', cluster (statefip)
	di "Region # is `k' "
	predict iv60_val_all_`k' if e(sample)
	predict se_iv60_val_all_`k' if e(sample), stdp 

	local pval = 2 * ttail(e(df_r), abs(_b[vetbin] / _se[vetbin]))
	local pval_star = ""
		if (`pval' < 0.001) local pval_star = "***"
		else if (`pval' < 0.01) local pval_star = "**"
		else if (`pval' < 0.05) local pval_star = "*"
		else if (`pval' < 0.10) local pval_star = "+"

	local row = 3

	if (`k' == 1) { //MIDWEST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel B`row' = ("`b'`pval_star'")
			
			
		local obs : di %9.0gc e(N)
		putexcel E`row' = ("`obs'")
			
		local row = `row' + 1
		local se = string(_se[vetbin_60], "%04.3f")
		putexcel B`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 2) { //NORTHEAST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel B`row' = ("`b'`pval_star'")
			
		local obs : di %9.0gc e(N)
		putexcel E`row' = ("`obs'")
			
		local row = `row' + 1
			
		local se = string(_se[vetbin_60], "%04.3f")
		putexcel B`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 3) { //SOUTH
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel B`row' = ("`b'`pval_star'")
			
		local obs : di %9.0gc e(N)
		putexcel E`row' = ("`obs'")
			
		local row = `row' + 1
		local se = string(_se[vetbin_60], "%04.3f")
		
		putexcel B`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 4) { //WEST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel B`row' = ("`b'`pval_star'")
		
		local obs : di %9.0gc e(N)
		putexcel E`row' = ("`obs'")
		
		local row = `row' + 1
		local se = string(_se[vetbin_60], "%04.3f")
			
		putexcel B`row' = ("(`se')")
		}
}

* WHITE  ***********************************************************************

forvalues k = 1/4 {
	ivreg ln_valueh_2018 age $state_fe (vetbin_60= inst_1927) [weight = perwt] if region_no == `k' & race == 1 , cluster (statefip)
	di "Region # is `k' & race == white "	
	predict iv60_val_white_`k' if e(sample)
	predict se_iv60_val_white_`k' if e(sample), stdp
	
	local pval = 2 * ttail(e(df_r), abs(_b[vetbin] / _se[vetbin]))
	local pval_star = ""
		if (`pval' < 0.001) local pval_star = "***"
		else if (`pval' < 0.01) local pval_star = "**"
		else if (`pval' < 0.05) local pval_star = "*"
		else if (`pval' < 0.10) local pval_star = "+"

	local row = 3

	if (`k' == 1) { //MIDWEST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel C`row' = ("`b'`pval_star'")
			
		local row = `row' + 1
		
		local se = string(_se[vetbin_60], "%04.3f")
		putexcel C`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 2) { //NORTHEAST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel C`row' = ("`b'`pval_star'")
			
		local row = `row' + 1
			
		local se = string(_se[vetbin_60], "%04.3f")
		putexcel C`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 3) { //SOUTH
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel C`row' = ("`b'`pval_star'")
			
		local row = `row' + 1
		local se = string(_se[vetbin_60], "%04.3f")
		
		putexcel C`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 4) { //WEST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel C`row' = ("`b'`pval_star'")
		
		local row = `row' + 1
		local se = string(_se[vetbin_60], "%04.3f")
			
		putexcel C`row' = ("(`se')")
		}
}

* BLACK ************************************************************************
forvalues k = 1/4 {
	ivreg ln_valueh_2018 age $state_fe (vetbin_60= inst_1927) [weight = perwt] if region_no == `k'  & race == 2, cluster (statefip)
	di "Region # is `k' & race == black "
	predict iv60_val_black_`k' if e(sample)
	predict se_iv60_val_black_`k' if e(sample), stdp
	
	local pval = 2 * ttail(e(df_r), abs(_b[vetbin] / _se[vetbin]))
	local pval_star = ""
		if (`pval' < 0.001) local pval_star = "***"
		else if (`pval' < 0.01) local pval_star = "**"
		else if (`pval' < 0.05) local pval_star = "*"
		else if (`pval' < 0.10) local pval_star = "+" 
	
	local row = 3
	
	if (`k' == 1) { //MIDWEST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel D`row' = ("`b'`pval_star'")
			
		local row = `row' + 1
		
		local se = string(_se[vetbin_60], "%04.3f")
		putexcel D`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 2) { //NORTHEAST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel D`row' = ("`b'`pval_star'")
			
		local row = `row' + 1
			
		local se = string(_se[vetbin_60], "%04.3f")
		putexcel D`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 3) { //SOUTH
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel D`row' = ("`b'`pval_star'")
			
		local row = `row' + 1
		local se = string(_se[vetbin_60], "%04.3f")
		
		putexcel D`row' = ("(`se')")
		}

	local row = `row' + 2

	if (`k' == 4) { //WEST
		local b = string(_b[vetbin_60], "%04.3f")
		putexcel D`row' = ("`b'`pval_star'")
		
		local row = `row' + 1
		local se = string(_se[vetbin_60], "%04.3f")
			
		putexcel D`row' = ("(`se')")
		}
	}


**************************************************************************************************
* FIGURE 7: IV Estimates of Linkages between Home Loan Guaranty and Home Value by Region (1960) * 
**************************************************************************************************

cap gen racevet = .
	replace racevet = 1 if vetbin_60 == 1 & race == 1
	replace racevet = 2 if vetbin_60 == 1 & race == 2
	replace racevet = 3 if vetbin_60 == 0 & race == 1
	replace racevet = 4 if vetbin_60 == 0 & race == 2
			
	cap label define racevet_cat 1"White Vets 1960" 2"Black Vets 1960" 3 "White Non-vets 1960" 4"Black Non-vets 1960" 
	label values racevet racevet_cat
	label var racevet "Race & Veteran Status 1960"
			
	codebook racevet region_no
	forvalues i = 1/4 {
		di "region # `i'"
		tabstat iv60_val_all_`i' , by(racevet) stat(mean sd)		
		}

cap drop racevet_mean*
forvalues j = 1/4 { 
    gen racevet_mean_`j'_white = .
    gen racevet_mean_`j'_black = .
	gen racevet_mean_`j' = .
	}

forvalues i = 0/1 { // Veteran status
forvalues k = 1/2 { // Race (1=White, 2=Black)
forvalues j = 1/4 { // Region
	if `k' == 1 { 
		sum iv60_val_white_`j' if vetbin_60 == `i' & race == `k' & region_no == `j'
        replace racevet_mean_`j'_white = r(mean) if vetbin_60 == `i' & race == `k' & region_no == `j' & racevet_mean_`j'_white == .
		}
	else if `k' == 2 { 
		sum iv60_val_black_`j' if vetbin_60 == `i' & race == `k' & region_no == `j'
		replace racevet_mean_`j'_black = r(mean) if vetbin_60 == `i' & race == `k' & region_no == `j' & racevet_mean_`j'_black == .
		}
		}
		}
		}
		
forvalues j = 1/4 {
    replace racevet_mean_`j' = racevet_mean_`j'_white if race == 1
    replace racevet_mean_`j' = racevet_mean_`j'_black if race == 2
	format racevet_mean_`j' %8.2f

}

cap drop lb_pi_* ub_pi*
forvalues j = 1/4 {
    gen lb_pi_`j'_white = .
    gen ub_pi_`j'_white = .
    gen lb_pi_`j'_black = .
    gen ub_pi_`j'_black = .
    gen lb_pi_`j' = .
    gen ub_pi_`j' = .
}

set more off
forvalues i = 0/1 { // Veteran status
forvalues k = 1/2 { // Race (1=White, 2=Black)
forvalues j = 1/4 { // Region	
	if `k' == 1 {
		replace lb_pi_`j'_white = iv60_val_white_`j' - invttail(e(df_r), 0.025) * se_iv60_val_white_`j' if vetbin_60 == `i' & race == `k' & region_no == `j' & lb_pi_`j'_white == .
		replace ub_pi_`j'_white = iv60_val_white_`j' + invttail(e(df_r), 0.025) * se_iv60_val_white_`j' if vetbin_60 == `i' & race == `k' & region_no == `j' & ub_pi_`j'_white == .
                
		sum lb_pi_`j'_white if vetbin_60 == `i' & race == `k' & region_no == `j'
        replace lb_pi_`j'_white = r(mean) if vetbin_60 == `i' & race == `k' & region_no == `j'

        sum ub_pi_`j'_white if vetbin_60 == `i' & race == `k' & region_no == `j'
        replace ub_pi_`j'_white = r(mean) if vetbin_60 == `i' & race == `k' & region_no == `j'
        }
	else if `k' == 2 {
		replace lb_pi_`j'_black = iv60_val_black_`j' - invttail(e(df_r), 0.025) * se_iv60_val_black_`j' if vetbin_60 == `i' & race == `k' & region_no == `j' & lb_pi_`j'_black == .
		replace ub_pi_`j'_black = iv60_val_black_`j' + invttail(e(df_r), 0.025) * se_iv60_val_black_`j' if vetbin_60 == `i' & race == `k' & region_no == `j' & ub_pi_`j'_black == .
                
		sum lb_pi_`j'_black if vetbin_60 == `i' & race == `k' & region_no == `j'
		replace lb_pi_`j'_black = r(mean) if vetbin_60 == `i' & race == `k' & region_no == `j'
		sum ub_pi_`j'_black if vetbin_60 == `i' & race == `k' & region_no == `j'
        replace ub_pi_`j'_black = r(mean) if vetbin_60 == `i' & race == `k' & region_no == `j'
		}
		}
		}
		}

forvalues j = 1/4 {
    replace lb_pi_`j' = lb_pi_`j'_white if race == 1
    replace lb_pi_`j' = lb_pi_`j'_black if race == 2
    replace ub_pi_`j' = ub_pi_`j'_white if race == 1
    replace ub_pi_`j' = ub_pi_`j'_black if race == 2
}
forvalues i = 1/4 {
	replace racevet_mean_`i' = exp(racevet_mean_`i')
	replace lb_pi_`i'= exp(lb_pi_`i')
	replace ub_pi_`i' = exp(ub_pi_`i')
	format racevet_mean_`i' %12.0fc 
	}
	
* Figure 7: IV Estimates of Linkages between Home Loan Guaranty and Home Value by Region (1960) 
	local regno 1 2 3 4
	local regname "Midwest Northeast South West"
	local regno_count : word count `regno'
	
	forvalues i = 1/`regno_count' {	
		
	local k: word 	`i' of `regname'
	
	graph twoway (bar racevet_mean_`i' racevet if racevet==1 & region_no ==`i' , color(eltgreen) barw(0.9)) ///
		(rcap lb_pi_`i' ub_pi_`i' racevet if racevet ==1 & region_no ==`i' ,  vertical lcolor(gs4)) ///
		(bar racevet_mean_`i' racevet if racevet==2  & region_no ==`i', color(lavender) barw(0.9)) ///
		(rcap lb_pi_`i' ub_pi_`i' racevet if racevet ==2  & region_no ==`i',  vertical lcolor(gs4)) ///
		(bar racevet_mean_`i' racevet if racevet==3  & region_no ==`i', color(eltgreen) barw(0.9)) ///
		(rcap lb_pi_`i' ub_pi_`i' racevet if racevet ==3  & region_no ==`i',  vertical lcolor(gs4)) ///
		(bar racevet_mean_`i' racevet if racevet==4 & region_no ==`i', color(lavender) barw(0.9)) ///
		(rcap lb_pi_`i' ub_pi_`i' racevet if racevet ==4 & region_no ==`i',  vertical lcolor(gs4)) 	///
		(scatter racevet_mean_`i' racevet, mlabel(racevet_mean_`i') msymbol(none) mlabposition(1)), ///
		ylabel(0(25000)150000, labsize(medium)) ytitle("Predicted Home Value ($2018)", size(medium)) ///
		xlabel( 1.5 "1960 Veteran" 3.5 "1960 Non-Veteran", noticks labsize(medium)) ///
		xtitle("") legend(off) title("`k'") name(iv60_region_`k', replace)
		
		di "region # `i', named `k'"
	

		if (`i' == 1) { //Midwest
			graph export "iv_bar60_color_val_`k'.png", replace
			}
		
		if (`i' == 2) { //northeast
			graph export "iv_bar60_color_val_`k'.png", replace
			}
			
		if (`i' == 3) { //south
			graph export "iv_bar60_color_val_`k'.png", replace
			}
		if (`i' == 4) { //west
			graph export "iv_bar60_color_val_`k'.png", replace
			}
		}
		
		* the combined graph of all regions 
		graph combine iv60_region_Midwest iv60_region_Northeast ///
			iv60_region_South iv60_region_West, rows(2) ///
			title (IV Estimates of Effect of HLG on Probability of Homeownership (1960))
			
		graph export "fig7.png", replace
		